<template>
    <vxe_table
      ref="baseTable"
      :options="options"
      @handleTableBtn="handleTableBtn"
    >
      <template #btn>
        <el-button type="primary" @click="add">添加一行</el-button>
      </template>
      <template #name="{ row }">
        <el-input v-model="row.name" placeholder="" clearable></el-input>
      </template>
      <template #select="{ row }">
        <el-select v-model="row.state" v-if="row.state">
          <el-option label="可用" :value="1"> </el-option>
          <el-option label="不可用" :value="-1"> </el-option>
        </el-select>
      </template>
    </vxe_table>
  </template>
  
  <script>
  import { addDicCode, delDicCode, editDicCode, getDicCode } from "@/api/baseset";
  import tableMixin from "@/mixins";
  import { hasBtnAuth, parseTime } from "@/utils";
  import { Message } from "element-ui";
  export default {
    mixins: [tableMixin],
    data() {
      return {
        options: {
          id: "adddwProduct",
          pageSize:100,
          hideExport: true,
          hideSearch: true,
          fixedParams: {
            type: 5,
          },
          apiFn: getDicCode,
          columns: [
            { field: "name", title: "名称", slot: "name", valueType: "input" },
            {
              field: "state",
              title: "状态",
              slot: "select",
            },
            {
              field: "createTime",
              title: "创建时间",
              type: "html",
              formatter: ({ row }) => parseTime(row.createTime),
            },
          ],
          btnWidth: 300,
          btn: [
          {
              title: "修改",
              handleName: "handleEdit",
              type: "success",
              show: (row) =>
                row.state && hasBtnAuth("btn_productAdmin_baseSet_edit"),
            },
            {
              title: "保存",
              handleName: "handleSave",
              show: (row) =>
                !row.state && hasBtnAuth("btn_productAdmin_baseSet_ad"),
            },
            {
              title: "删除",
              handleName: "handleDel",
              type: "danger",
              show: (row) => hasBtnAuth("btn_productAdmin_baseSet_del"),
            },
          ],
        },
      };
    },
    methods: {
      add() {
        this.$refs.baseTable.tableData.unshift({});
      },
      async handleSave(row) {
        if (!row.name) {
          Message.error("请输入名称");
          return;
        }
        const res = await addDicCode({
          name: row.name,
          type: 5,
        });
        if (!res.status) {
          Message.success("添加成功");
          this.success();
        }
      },
      handleDel(row) {
        this.$del({ code: row.code }, delDicCode);
      },
      async handleEdit(row) {
        if (!row.name) {
          Message.error("请输入名称");
          return;
        }
        const res = await editDicCode({
          name: row.name,
          type: 5,
          code: row.code,
          state: row.state,
        });
        if (!res.status) {
          Message.success("修改成功");
          this.success();
        }
      },
    },
  };
  </script>
  